package com.springstudy.web;


//import com.springstudy.utils.WebApplicationContextUtils;
import com.springstudy.service.AccountService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;

/**
 * @author Tamako
 * @since 2025/1/13 13:15
 */
@WebServlet(urlPatterns = "/accountServlet")
public class AccountServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//        AccountService accountService = context.getBean(AccountService.class);
        ServletContext servletContext = req.getServletContext();
//        ApplicationContext context = (ApplicationContext) servletContext.getAttribute("applicationContext");
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        AccountService accountService = context.getBean(AccountService.class);
        accountService.transferMoney("tom", "lucy", 500);
    }


}
